package com.kaifamiao.typehandler;

import org.apache.ibatis.type.*;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int index, LocalDate date, JdbcType type) throws SQLException {
        java.sql.Date birthdate = date != null ? java.sql.Date.valueOf( date ) : null ;
        ps.setDate( index, birthdate );
    }

    @Override
    public LocalDate getNullableResult(ResultSet rs, String label) throws SQLException {
        java.sql.Date date = rs.getDate( label );
        LocalDate birthdate = date != null ? date.toLocalDate() : null;
        return birthdate;
    }

    @Override
    public LocalDate getNullableResult(ResultSet rs, int index) throws SQLException {
        java.sql.Date date = rs.getDate( index );
        LocalDate birthdate = date != null ? date.toLocalDate() : null;
        return birthdate;
    }

    @Override
    public LocalDate getNullableResult(CallableStatement cs, int index) throws SQLException {
        java.sql.Date date = cs.getDate( index );
        LocalDate birthdate = date != null ? date.toLocalDate() : null;
        return birthdate;
    }

}
